package cn.edu.cqie.maven_onlinexem.controller;


import cn.edu.cqie.maven_onlinexem.bean.AjaxResult;
import cn.edu.cqie.maven_onlinexem.bean.Student;
import cn.edu.cqie.maven_onlinexem.service.StudentService;
import cn.edu.cqie.maven_onlinexem.until.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {
    @Resource
    StudentService studentService;
    @RequestMapping("index")
    public ModelAndView toIndex(){
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }
    @RequestMapping("login")
    public ModelAndView login(){
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }
    @RequestMapping("register")
    public ModelAndView register(){
        ModelAndView modelAndView = new ModelAndView("register");
        return modelAndView;
    }
    @RequestMapping("userSelect")
    public ModelAndView userSelect(){
        ModelAndView modelAndView = new ModelAndView("userSelect");
        return modelAndView;
    }

    /**
     * 用户管理模块
     * @return
     */
    /**
     * 用户登录
     * @param student
     * @param session
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("doAjaxLogin")
    public Object doAjaxLogin(Student student, HttpSession session, HttpServletResponse response){
        String passwordMD5 = MD5.MD5(student.getStudent_password());
        student.setStudent_password(passwordMD5);
        boolean b = studentService.login(student);
        AjaxResult ajaxResult = new AjaxResult();
        if(b){
            session.setAttribute("loginUser",student.getStudent_name());
            ajaxResult.setSuccess(true);
        }
        else {
            ajaxResult.setSuccess(false);
        }
        System.out.println(ajaxResult);
        return ajaxResult;
    }

    /**
     * 用户注册
     * @param student
     * @return ajaxResult true 注册成功 fales 注册失败
     */
    @ResponseBody
    @RequestMapping("doAjaxRegister")
    public Object doAjaxRegister(Student student) {

        //创建一个AjaxResult对象
        AjaxResult ajaxResult = new AjaxResult();
        System.out.println(student.getStudent_name());
        String passwordMD5 = MD5.MD5(student.getStudent_password());
        System.out.println(passwordMD5);
        student.setStudent_password(passwordMD5);
        boolean b = studentService.isFindStudent(student);
        if (b) {
            //该用户已经被注册
                ajaxResult.setSuccess(false);
            } else {
                try {
                    studentService.insertStudent(student);
                    ajaxResult.setSuccess(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    ajaxResult.setSuccess(false);
                }
            }
            return ajaxResult;
        }

    @ResponseBody
    @RequestMapping("doAjaxUserSelect")
    public Object doAjaxUserSelect(Student student, HttpSession session, HttpServletResponse response){

        AjaxResult ajaxResult = new AjaxResult();
        List<Student> userList = studentService.findAllStudent(student);
        if(userList.size()>0){
              session.setAttribute("selectUser",userList);
              ajaxResult.setSuccess(true);
         }
        else {
            ajaxResult.setSuccess(false);
        }
        System.out.println(ajaxResult);
        return ajaxResult;
}
    /**、
     * 删除用户
     * @param student
     * @return
     */
    @ResponseBody
    @RequestMapping("doAjaxDelete")
    public Object doAjaxDelete(Student student) {
        return null;
    }
}




